package com.sy.olddriver.service.impl;

import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.dao.ProductDao;
import com.sy.olddriver.dao.impl.ProductDaoImpl;
import com.sy.olddriver.dto.Page;
import com.sy.olddriver.dto.Result;
import com.sy.olddriver.entity.Product;
import com.sy.olddriver.exception.OldDriverException;
import com.sy.olddriver.service.HistoryService;
import com.sy.olddriver.service.ProductService;
import org.apache.commons.lang.StringUtils;

/***
 * ProductService接口的实现类
 * @author Administrator
 */
public class ProductServiceImpl implements ProductService {

    private ProductDao productDao = new ProductDaoImpl();

    private HistoryService historyService = new HistoryServiceImpl();

    @Override
    public Result getNewestProducts() {
        Result result = new Result();
        try {
            result.setData(productDao.getNewestProducts(AppConstant.NEWEST_PRODUCTS_SHOW_COUNT));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public Result getHotestProducts() {
        Result result = new Result();
        try {
            result.setData(productDao.getHotestProducts(AppConstant.HOTEST_PRODUCTS_SHOW_COUNT));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public Result searchProductsByCondtiions(String cid, String q, String pageNo) {
        Result result = new Result();
        try {
            Page<Product> page = new Page<>();

            int pageNoValue;
            //如果前端没有传递页码，则默认为第1页
            if (StringUtils.isBlank(pageNo)) {
                pageNoValue = AppConstant.SEARCH_RESULT_DEFAULT_PAGENO;
            } else {
                pageNoValue = Integer.parseInt(pageNo);
            }
            page.setPageNo(pageNoValue);
            page.setPageSize(AppConstant.SEARCH_RESULT_PAGE_SIZE);

            //获取总数量
            Long totalCount = productDao.getProductsCountByCondition(cid, q);
            page.setTotal(totalCount);
            //计算页数
            Long totalCountModPageSize = totalCount % AppConstant.SEARCH_RESULT_PAGE_SIZE;
            Long totalCountDividePageSize = totalCount / AppConstant.SEARCH_RESULT_PAGE_SIZE;
            page.setPageCount(totalCountModPageSize = (totalCountModPageSize == 0) ? totalCountDividePageSize : totalCountDividePageSize + 1);
            //计算该页的起始索引
            Integer start = (pageNoValue - 1) * AppConstant.SEARCH_RESULT_PAGE_SIZE;
            page.setData(productDao.getProductsByCondition(cid, q, start));
            result.setData(page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrMsg(e.getMessage());
        }

        return result;
    }

    @Override
    public Result getProductById(String pid) {
        Result result = new Result();
        try {
            if (StringUtils.isBlank(pid)) {
                throw new OldDriverException("商品编号为空！");
            }
            Product product = productDao.getProductById(pid);
            if (null == product) {
                throw new OldDriverException("未找到对应的商品");
            } else {
                //1.对商品的点击量要加1
                productDao.addClickCount(pid);
                //2.添加一条浏览后的历史记录
                historyService.addHistory(pid);
                result.setData(product);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrMsg(e.getMessage());
        }
        return result;
    }
}
